/**   
 * @Title: LotteryRecordDetailMoreAct.java 
 * @Package: com.esun.rabbit2.viewer.mainact.mylotteryact 
 * @Description: (方案详情) 
 * @Author: Billy qigs@500wan.com   
 * @Date: 2011-7-1 下午01:53:16  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.rabbit2.viewer.mainact.mylotteryact.lotteryrecord;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.esun.config.Config;
import com.esun.config.Key;
import com.esun.network.ConnectionChangeReceiver;
import com.esun.ui.R;
import com.esun.util.log.EDebug;

/**
 * @ClassName: LotteryRecordDetailMoreAct
 * @Description: (方案详情)
 * @Author: Billy qigs@500wan.com
 * @Date: 2011-7-1 下午01:53:16
 */
public class LotteryRecordDetailMoreAct extends Activity implements OnClickListener {
	private WebView webViewShowMore;
	private ScrollView scrollViewContent;
	private LinearLayout linearFootball;
	private Bundle bundle;
	private String url;
	private String lotid;
	private String[] strContent;
	private ImageButton btnBack;
	private RelativeLayout relativeLayoutProgress;
	private String THREE_SPACE = "\t ";
	private TextView tvContent;

	/** 2011-08-10 city add ,thanks for mable's help */
	private LinearLayout linearNOText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lotteryrecord_detailmore_act);

		initUI();

	}

	public void initUI() {

		webViewShowMore = (WebView) findViewById(R.id.lotteryrecord_detailmoreact_webbiew);
		tvContent = (TextView) findViewById(R.id.lotteryrecord_detailmoreact_tv_cotent);
		scrollViewContent = (ScrollView) findViewById(R.id.lotteryrecord_detailmoreact_scrollview);
		linearFootball = (LinearLayout) findViewById(R.id.lotteryrecord_detailmore_act_linear_football);
		btnBack = (ImageButton) findViewById(R.id.lotteryrecord_detailmoreact_btn_back);
		btnBack.setOnClickListener(this);

		relativeLayoutProgress = (RelativeLayout) findViewById(R.id.lotteryrecorddetailmoreact_relative_progress);
		relativeLayoutProgress.setVisibility(View.VISIBLE);

		linearNOText = (LinearLayout) findViewById(R.id.lotteryrecorddetailmoreact_linear_noText);

		// progressDialog =
		// ViewerUtil.showProgressDialog(LotteryRecordDetailMoreAct.this,
		// getResources().getString(R.string.loading_text));
		// progressDialog.show();

		bundle = this.getIntent().getExtras();
		lotid = bundle.getString(Key.KEY_LOTID);

		if (bundle.getString("classify").equals("web")) {
			url = bundle.getString("detailurl");
			EDebug.o(this, url);
			EDebug.o(this, "web" + "  " + url);
			webViewShowMore.setVisibility(View.VISIBLE);
			webViewShowMore.setWebViewClient(new WebViewClient() {

				@Override
				public void onPageFinished(WebView view, String url) {
					// TODO Auto-generated method stub
					super.onPageFinished(view, url);
					relativeLayoutProgress.setVisibility(View.GONE);
					// progressDialog.cancel();
				}

				@Override
				public void onPageStarted(WebView view, String url, Bitmap favicon) {
					// TODO Auto-generated method stub
					super.onPageStarted(view, url, favicon);
				}

			});
			webViewShowMore.loadUrl(url);
		} else if (bundle.getString("classify").equals("txt")) {
			EDebug.o(this, "txt");
			scrollViewContent.setVisibility(View.VISIBLE);
			// tvContent.setVisibility(View.VISIBLE);
			strContent = bundle.getStringArray("content");
			EDebug.o(this, strContent);
			for (int i = 0; i < strContent.length; i++) {
				EDebug.o(this, strContent[i]);
				addItemToLinearNoText(strContent[i]);
				tvContent.setText(tvContent.getText() + strContent[i] + '\n');
			}
			relativeLayoutProgress.setVisibility(View.GONE);
		} else if (bundle.getString("classify").equals("football")) {
			EDebug.o(this, "football");
			scrollViewContent.setVisibility(View.VISIBLE);
			addChildContent(bundle.getStringArrayList("matchnum"), bundle.getStringArrayList("homesxname"), bundle.getStringArrayList("awaysxname"), bundle.getStringArrayList("score"),
					bundle.getStringArrayList("rangqiu"), bundle.getStringArrayList("spf"), bundle.getStringArrayList("result"), bundle.getStringArrayList("isdanma"),
					bundle.getStringArrayList("peilv"));

			relativeLayoutProgress.setVisibility(View.GONE);
		}
	}

	private void addItemToLinearNoText(String strItem) {
		// TODO Auto-generated method stub
		View viewListItem = LotteryRecordDetailMoreAct.this.getLayoutInflater().inflate(R.layout.lotteryrecord_linear_itemfordetail, null);
		TextView tvItem = (TextView) viewListItem.findViewById(R.id.lotteryrecordlinearitemfordetail_tv_item);

		SpannableString sp;
		EDebug.o(LotteryRecordDetailMoreAct.this, " str item is " + strItem + "!!!!!!!!!!!!!!!!");
		if (lotid.equals(Config.SHUANGSEQIU) || lotid.equals(Config.DALETOU)) {
			EDebug.o(this, "双色球大乐透，区分红篮球");
			strItem = strItem.replace(Config.VERTRAL_LINE, THREE_SPACE);
			sp = new SpannableString(strItem);
			try {
				sp.setSpan(new ForegroundColorSpan(Color.RED), 0, strItem.indexOf(THREE_SPACE), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
				sp.setSpan(new ForegroundColorSpan(Color.BLUE), strItem.indexOf(THREE_SPACE) + 1, strItem.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
			} catch (Exception e) {
				// TODO: handle exception
			}
		} else if (lotid.equals(Config.SHENGFUCAI) || lotid.equals(Config.FUCAI3D) || lotid.equals(Config.RENXUANJIU)) {
			EDebug.o(this, "胜负彩、福彩、任九不需要区分红篮球");
			sp = new SpannableString(strItem);
		} else {
			EDebug.o(this, "其他彩种");
			sp = new SpannableString(strItem);
		}

		tvItem.setText(sp);
		linearNOText.addView(viewListItem);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lotteryrecord_detailmoreact_btn_back:
			LotteryRecordDetailMoreAct.this.finish();
			break;
		}
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		this.unregisterReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		this.registerReceiver(ConnectionChangeReceiver.getConnectionChangeReceiver(), ConnectionChangeReceiver.getIntentFilter());
	}

	// 添加比赛内容
	private void addChildContent(ArrayList<String> matchNum, ArrayList<String> homeTeam, ArrayList<String> visitingTeam, ArrayList<String> score, ArrayList<String> rangQiu,
			ArrayList<String> lotContent, ArrayList<String> result, ArrayList<String> isDanMa, ArrayList<String> peiLvList) {
		for (int i = 0; i < matchNum.size(); i++) {

			View viewContent = LotteryRecordDetailMoreAct.this.getLayoutInflater().inflate(R.layout.lotteryrecord_detailfootball_procontent_table, null);
			// 赛事编号
			TextView tvMatchNum = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_matchNum);
			tvMatchNum.setText(matchNum.get(i));
			// 主队
			TextView tvHomeTeam = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_homeTeam);
			tvHomeTeam.setText(homeTeam.get(i).replace(" ", ""));
			// 让球
			TextView tvRangQiu = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_rangQiu);
			tvRangQiu.setText(rangQiu.get(i));
			// 客队
			TextView tvVisitingTeam = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_visitingTeam);
			tvVisitingTeam.setText(visitingTeam.get(i).replace(" ", ""));
			// 比分和比赛结果
			TextView tvScore = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_score);
			TextView tvResult = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_result);
			if (!score.get(i).equals("")) {
				tvScore.setText(score.get(i));
				tvResult.setText("(" + result.get(i) + ")");
			} else {
				tvScore.setText("未比赛");
				tvResult.setText("");
			}
			// 投注内容
			TextView tvLotteryContentOne = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_one);
			TextView tvLotteryContentTwo = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_two);
			TextView tvLotteryContentThree = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_three);
			String[] content = lotContent.get(i).split(",");
			String[] peiLv = peiLvList.get(i).split(",");
			if (content.length >= 1) {
				if (peiLv[0] != null && !peiLv[0].equals("")) {
					tvLotteryContentOne.setText(content[0] + "(" + peiLv[0] + ")");
				} else {
					tvLotteryContentOne.setText(content[0]);
				}
				if (content[0].equals(result.get(i))) {
					tvLotteryContentOne.setTextColor(getResources().getColor(R.color.kjxx_red));
				}
				if (content.length >= 2) {
					if (peiLv[1] != null && !peiLv[1].equals("")) {
						tvLotteryContentTwo.setText("," + content[1] + "(" + peiLv[1] + ")");
					} else {
						tvLotteryContentTwo.setText("," + content[1]);
					}
					if (content[1].equals(result.get(i))) {
						tvLotteryContentTwo.setTextColor(getResources().getColor(R.color.kjxx_red));
					}
					if (content.length >= 3) {
						if (peiLv[2] != null && !peiLv[2].equals("")) {
							tvLotteryContentThree.setText("," + content[2] + "(" + peiLv[2] + ")");
						} else {
							tvLotteryContentThree.setText("," + content[2]);
						}
						if (content[2].equals(result.get(i))) {
							tvLotteryContentThree.setTextColor(getResources().getColor(R.color.kjxx_red));
						}
					}
				}
			}
			// 胆
			TextView tvDanMa = (TextView) viewContent.findViewById(R.id.lotteryrecord_detailfootball_procontent_table_tv_dan);
			if (isDanMa.get(i).equals("1")) {
				tvDanMa.setVisibility(View.VISIBLE);
			}

			linearFootball.addView(viewContent);
		}
	}
}
